<template>
	<view>
		<view class="swiper">
			<u-swiper :list="swiperList" height="300"></u-swiper>
			</view>
		<u-gap height="20" bg-color="#F7F7F7"></u-gap>
		<view class="news">
			<u-tabs :list="tabList" :current="tabCurrent" @change="tabChange" class="top-bar"></u-tabs>
			<view class="newsList">
				<view class="news-item" v-for="item in newsList">
					<view class="left">
						<text class="title">{{item.title}}</text>
						<view class="bar">
							<text>{{item.readNum}} 看过</text>
							<text>{{item.likeNum}} 点赞</text>
						</view>
					</view>
					<view class="right">
						<navigator :url="`/pages/news-detail/news-detail?id=${item.id}`">
							<image :src="baseUrl + item.cover" mode=""></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList: [],
				baseUrl: 'http://124.93.196.45:10001/',
				tabList: [],
				tabCurrent: 0,
				newsList: []
			};
		},
		onLoad() {
			this.getSwiperData(),
			this.getNewsType()
		},
		methods: {
			async getSwiperData(){
				const res = await this.$http({
					url: '/prod-api/api/rotation/list',
					type: '2'
				})
				this.swiperList = res.data.rows.map(item => this.baseUrl + item.advImg)
			},
			async getNewsType(){
				const res = await this.$http({
					url: '/prod-api/press/category/list'
				})
				console.log(res)
				this.tabList = res.data.data.map((item) => ({name: item.name, type: item.id}))
				this.getNewsList()
			},
			async getNewsList(){
				const res = await this.$http({
					url: '/prod-api/press/press/list',
					data: {
						type: this.tabList[this.tabCurrent].type
					}
				})
				this.newsList = res.data.rows
				// 按日期降序排序
				this.newsList = this.newsList.sort((a, b) => {
					return new Date(b.publishDate).getTime() - new Date(a.publishDate).getTime()
				})
				console.log(this.newsList)
			},
			tabChange(index){
				this.tabCurrent = index
				this.getNewsList()
			}
		},
		computed: {
			sortData(a, b){
				return new Date(b.publishDate).getTime() - new Date(a.publishDate).getTime()
			}
		}
	}
</script>

<style lang="scss">
view{
	height: 100%;
	background-color: #fff;
	.swiper{
		padding: 20rpx;
	}
	.newsList{
		display: flex;
		flex-direction: column;
		padding: 20rpx;
		width: 100%;
		.news-item{
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			border-top: 2rpx solid #e4e7ed;
			padding: 20rpx 0;
			height: fit-content;
			position: relative;
			.left{
				display: flex;
				flex-direction: column;
				.title{
					font-size: 32rpx;
					line-height: 1.7;
					font-weight: bold;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
					width: 460rpx;
					height: 110rpx;
				}
				.bar{
					margin-top: 40rpx;
					font-size: 22rpx;
					color: #909399;
					display: flex;
					flex-direction: row;
					justify-content: start;
					:nth-child(2){
						margin-left: 20rpx;
					}
				}
			}
			.right{
				display: flex;
				align-items: center;
				justify-content: center;
				image{
					width: 220rpx;
					height: 180rpx;
				}
			}
		}
	}
}
</style>
